package lk.icta.core.dao.impl;

import lk.icta.common.dao.impl.BaseDAO;
import lk.icta.core.dao.CalenderScheduleDAO;
import lk.icta.core.entity.CalenderSchedule;
import lk.icta.core.entity.CalenderScheduleType;
import lk.icta.user.entity.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.Date;
import java.util.List;

/**
 * @author Kusal Samarakoon
 */
public class CalenderScheduleDAOImpl extends BaseDAO implements CalenderScheduleDAO {

    @Transactional(propagation = Propagation.REQUIRED)
    public void addCalenderSchedule(CalenderSchedule calenderSchedule, User user) {
        logger.debug("Adding a Calender Schedule {}", calenderSchedule.getCalenderId());
        calenderSchedule.getLifeCycleInfo().setCreatedUser(user);
        calenderSchedule.getLifeCycleInfo().setCreatedTimeStamp(new Date());
        calenderSchedule.getLifeCycleInfo().setLastUpdatedTimeStamp(new Date());
        calenderSchedule.getLifeCycleInfo().setLastUpdatedUser(user);
        em.persist(calenderSchedule);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateCalenderSchedule(CalenderSchedule calenderSchedule, User user) {
        logger.debug("Updating a Calender Schedule {}", calenderSchedule.getCalenderId());
        calenderSchedule.getLifeCycleInfo().setLastUpdatedTimeStamp(new Date());
        calenderSchedule.getLifeCycleInfo().setLastUpdatedUser(user);
        em.merge(calenderSchedule);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteCalenderSchedule(String id) {
        logger.debug("Deliting a Calender Schedule {}", id);
        CalenderSchedule schedule = em.find(CalenderSchedule.class, id);
        if (schedule != null) {
            em.remove(schedule);
            em.flush();
        }
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public CalenderSchedule getCalenderScheduleById(String id, User user) {
        logger.debug("Loading CalenderSchedule [Id: {}]", id);
        return em.find(CalenderSchedule.class, id);
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public List<CalenderSchedule> getAllCalenderSchedule(User user) {
        logger.debug("List CalenderSchedule records");
        Query q = em.createNamedQuery("getAllCalenderSchedule");
        q.setParameter("user", user);
        return q.getResultList();
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public List<CalenderSchedule> getAllCalenderScheduleByUser(User user) {
        logger.debug("List CalenderSchedule records by user");
        Query q = em.createNamedQuery("getAllCalenderScheduleByUser");
        q.setParameter("user", user);
        return q.getResultList();
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public List<CalenderSchedule> getAllCalenderScheduleByCalenderScheduleType(CalenderScheduleType scheduleType, User user) {
        logger.debug("List CalenderSchedule records by CalenderScheduleType");
        Query q = em.createNamedQuery("getAllCalenderScheduleByCalenderScheduleType");
        q.setParameter("scheduleType", scheduleType);
        return q.getResultList();
    }
}



